package com.wxlsoft.config.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class EsbAppInfo {

	/**
	 * 协议ID
	 */
	private String id;
	
	/**
	 * 系统ID
	 */
	private String systemId;
	
	/**
	 * 系统ID
	 */
	private String systemText;
	
	/**
	 * 所属微服务应用名
	 */
	private String serviceName;
	
	public static List<EsbAppInfo> parse(JsonNode appData, JsonNode simpleInfos) {
		Map<String, String> mapping = new HashMap<>();
		for (JsonNode app : (ArrayNode) simpleInfos) {
			String id = app.get("id").asText();
			String text = app.get("text").asText();
			mapping.put(id, text);
		}
		
		List<EsbAppInfo> apps = new ArrayList<>();
		for (JsonNode app : (ArrayNode) appData.get("records")) {
			String id = app.get("id").asText();
			String serviceName = app.get("serviceName").asText();
			String systemId = app.get("serviceSystem").asText();
			String systemText = mapping.get(systemId);
			apps.add(new EsbAppInfo(id, systemId, systemText, serviceName));
		}
		return apps;
	}
	
}
